package com.woniuxy.intellihome.controller.admin;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.intellihome.entity.GoodsSpu;
import com.woniuxy.intellihome.entity.ResponseEntity;
import com.woniuxy.intellihome.entity.Spec;
import com.woniuxy.intellihome.service.SpecService;
import com.woniuxy.intellihome.utils.UtilsTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-08-11
 */
@RestController
@Slf4j
@RequestMapping("admin/spec")
public class SpecController {
    @Autowired
    private SpecService specService;

    /**
     * 根据spu的id得到某一goods规格
     * @param id goods的id
     * @return 规格list
     */
    @RequestMapping("getbygoodsid")
    public ResponseEntity<List<Spec>> getByGoodsId(int id){
        List<Spec> specs = specService.getByGoodsId(id);
        return new ResponseEntity<>(specs);
    }

    @RequestMapping("add")
    public ResponseEntity<Spec> add(@RequestBody Spec spec){
        spec.setStatus("y");
        log.debug("add spec:{}",spec);
        specService.save(spec);
        return new ResponseEntity<>(spec);
    }

    //上下架
    @RequestMapping("upDown")
    public ResponseEntity<Spec> changeStatus(int id){
        Spec spec = specService.getDetailedById(id);
        if("y".equals( spec.getStatus())){
            spec.setStatus("n");
        }else{
            spec.setStatus("y");
        }
        specService.updateById(spec);
        return new ResponseEntity<>(spec);
    }
}

